﻿#region

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Radovici.SharedLibrary;
using YouTubePlaylist.Models;

#endregion

namespace YouTubePlaylist.Views
{
    public partial class FinderCtrl : UserControl
    {
        public FinderCtrl()
        {
            InitializeComponent();
            DataContext = new SearchModel();
            var root = Application.Current.RootVisual as FrameworkElement;
            if (root != null)
            {
                root.SizeChanged += delegate { UpdateSize(); };
            }
            Loaded += delegate { UpdateSize(); };
        }

        private void UpdateSize()
        {
            var root = Application.Current.RootVisual as FrameworkElement;
            if (root != null)
            {
                try
                {
                    GeneralTransform gt = TransformToVisual(root);
                    Point offset = gt.Transform(new Point(0, 0));
                    MaxHeight = root.ActualHeight - offset.Y;
                    MaxWidth = root.ActualWidth - offset.X;
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("FinderCtrl().UpdateSize() exception = " + ex);
                }
            }
        }

        private void OnSearchChanged(object sender, TextChangedEventArgs e)
        {
            var txt = sender as TextBox;
            if (txt != null)
            {
                var dc = DataContext as SearchModel;
                if (dc != null)
                {
                    dc.GetTracks(txt.Text);
                }
            }
        }

        private void OnDoubleClicked(object sender, MouseButtonEventArgs e)
        {
            var data = sender as DataGrid;
            if (data != null)
            {
            }
        }

        private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            var grid = sender as DataGrid;
            if (grid != null)
            {
            }
        }
    }
}